package cn.xinfei.xdecision.engine.api;

import cn.xinfei.xdecision.common.utils.util.JsonUtil;
import cn.xinfei.xdecision.common.utils.util.JsonUtils;
import cn.xinfei.xdecision.data.CreditDataApiInterface;
import cn.xinfei.xdecision.model.CreditVarRequest;
import cn.xinfei.xdecision.model.CreditVarResponse;
import cn.xinfei.xdecision.model.VariableApiRequest;
import cn.xinfei.xdecision.model.VariableApiResponse;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("engine/credit")
public class XDecisionCreditApi {


    @Autowired
    private CreditDataApiInterface creditCollect;


    /**
     * 征信申请接口
     *
     * @param apiRequest
     * @return
     */
    @RequestMapping(value = "/apply", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "征信申请接口")
    public CreditVarResponse apply(@RequestBody CreditVarRequest apiRequest) {
        log.info("apply request:{}", JsonUtil.toJson(apiRequest));
        long start = System.currentTimeMillis();
        CreditVarResponse dataApiResponse = null;
        try {
            dataApiResponse = creditCollect.creditCollect(apiRequest);
            log.info("DataCreditApplyHandler.exec response={}", JsonUtil.toJson(dataApiResponse));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            long end = System.currentTimeMillis();
            log.info("creditApplyDuration={}", end - start);
        }

        return dataApiResponse;
    }


    /**
     * 征信取数
     *
     * @param apiRequest
     * @return
     */
    @ApiOperation(value = "征信取数")
    @RequestMapping(value = "/征信取数", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")

    public VariableApiResponse fetch(@RequestBody VariableApiRequest apiRequest) {
        log.info("fetch request:{}", JsonUtils.toJsonString(apiRequest));
        long start = System.currentTimeMillis();
        VariableApiResponse dataApiResponse = null;
        try {
            dataApiResponse = creditCollect.fetchCreditVars(apiRequest);
            log.info("DataCreditFetchHandler.exec response={}", JsonUtils.toJsonString(dataApiResponse));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            long end = System.currentTimeMillis();
            log.info("creditFetchDuration={}", end - start);
        }
        return dataApiResponse;
    }


}
